package com.code.generate;

import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConnectionUtil {
	private String className;
	private String url;
	private String userName;
	private String password;

	public ConnectionUtil(String className, String url, String userName, String password) {
		this.className = className;
		this.url = url;
		this.userName = userName;
		this.password = password;
	}

	public Connection getConnection() {
		Connection con = null;
		try {
			Class.forName(className).newInstance();
			con = DriverManager.getConnection(url, userName, password);
		} catch (Exception e) {
			System.err.println("Exception: " + e.getMessage());
		}
		return con;
	}

	public List<Map<String, Object>> executeQuery(String sql) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Connection con = null;
		try {
			Class.forName(className).newInstance();
			con = DriverManager.getConnection(url, userName, password);
			if (!con.isClosed()) {
				Statement st = con.createStatement();
				ResultSet rs = st.executeQuery(sql);
				ResultSetMetaData rsmd = rs.getMetaData();
				String[] fields = new String[rsmd.getColumnCount()];
				for (int i = 0; i < rsmd.getColumnCount(); i++) {
					fields[i] = rsmd.getColumnName(i + 1);
					// System.out.println("fields : " + fields[i]);
				}
				// 循环读取数据
				while (rs.next()) {
					// 打印数据
					Map<String, Object> obj = new HashMap<String, Object>();
					for (int i = 0; i < fields.length; i++) {

						obj.put(fields[i], rs.getObject(i + 1));
						// System.out.println("fields : " + fields[i] +
						// " values : " + obj.get(fields[i]));

					}
					result.add(obj);
				}
				// 关闭
				con.close();
			}
		} catch (Exception e) {
			System.err.println("Exception: " + e.getMessage());
            JOptionPane.showMessageDialog(null, e.getMessage(), "错误",JOptionPane.ERROR_MESSAGE);
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public boolean update(String sql) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			Class.forName(className).newInstance();
			conn = DriverManager.getConnection(url, userName, password);
			conn.setReadOnly(false);
			stmt = conn.prepareStatement(sql);
			stmt.executeUpdate();
		} catch (Exception e) {
			System.err.println("Exception: " + e.getMessage());
			try {
				conn.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			return false;
		} finally {
			try {
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return true;
	}
}
